package cn.may.handle;

import cn.may.exception.StudentException;
import cn.may.domain.Result;
import cn.may.utils.ResultUtil;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartException;

/**
 * @author mayuanbao
 * @date 2019-06-04 20:44
 */
//@ControllerAdvice
public class ExceptionHandle {
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e) {
        if (e instanceof StudentException) {
            StudentException studentException = (StudentException) e;
            return ResultUtil.error(studentException.getCode(), studentException.getMessage());
        } else if (e instanceof MultipartException) {
            MultipartException multipartException = (MultipartException) e;
            return ResultUtil.error(110, multipartException.getMessage());
        } else {
            return ResultUtil.error(-1, e.getMessage());
        }
    }
}
